package com.example.shoppingmallclient;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

@SuppressWarnings("all")
public class ShowProductDetail extends AppCompatActivity {

    private Integer productId = 2; //todo!!!!!!!!!!!!!!
    private ImageView ivPhoto;
    private TextView tvBrand;
    private TextView tvSize;
    private TextView tvPrice;
    private TextView tvBaseInformation;
    private TextView tvDetail;
    private TextView tvDiscount;
    private Button btnInsert;
    private String message;

    private String brand;
    private String size;
    private String price;
    private String baseInformation;
    private String detail;
    private String discount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.product_detail);

        findViews();
        new showDetail().execute();

    }

    private void findViews(){
        ivPhoto = findViewById(R.id.iv_photo);
        tvBaseInformation = findViewById(R.id.tv_base_information);
        tvBrand = findViewById(R.id.tv_brand);
        tvSize = findViewById(R.id.tv_size);
        tvDetail = findViewById(R.id.tv_detail);
        tvPrice = findViewById(R.id.tv_price);
        tvDiscount = findViewById(R.id.tv_discount);
        btnInsert = findViewById(R.id.btn_insert);
    }

    private class showDetail extends AsyncTask<Object, Object, Bitmap>{

        @Override
        protected Bitmap doInBackground(Object[] objects) {
            try {
                URL url = new URL("http://10.7.88.84:8080/DressServlet");
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");
                OutputStream os = connection.getOutputStream();

                os.write(productId.toString().getBytes());
                os.flush(); //刷新缓存

                //接收服务器发来的消息
                InputStream is = connection.getInputStream();
                StringBuffer stringBuffer = new StringBuffer();
                int len = 0;
                byte[] bytes = new byte[1024];
                while ((len = is.read(bytes)) != -1){
                    stringBuffer.append(new String(bytes, 0, len));
                }
                String[] strings = stringBuffer.toString().split("#");

                baseInformation = strings[2];
                price = strings[3];
                detail = strings[4];
                brand = strings[5];
                size = strings[6];
                discount = strings[7];

                String image = (String) strings[1];
                URL imgUrl = new URL(image);
                InputStream inputStream = imgUrl.openStream();
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

                return bitmap;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            ivPhoto.setImageBitmap(bitmap);
            tvBaseInformation.setText(baseInformation);
            tvPrice.setText(price);
            tvDetail.setText(detail);
            tvBrand.setText("品牌：" + brand);
            tvSize.setText("尺码：" + size);
            if("true".equals(discount)){
                tvDiscount.setText("推荐商品(๑•̀ㅂ•́)و✧");
            }
        }
    }
}